/*
 * 项目名称:platform-plus
 * 类名称:AlgSubjectController.java
 * 包名称:com.platform.modules.alg.controller
 *
 * 修改履历:
 *     日期                       修正者        主要内容
 *     2022-08-06 09:57:11        chengqiuming     初版做成
 *
 * Copyright (c) 2019-2019 贝医
 */
package com.platform.modules.alg.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.common.annotation.SysLog;
import com.platform.common.utils.RestResponse;
import com.platform.modules.alg.dto.AlgSubject;
import com.platform.modules.alg.entity.AlgSubjectEntity;
import com.platform.modules.alg.service.AlgSubjectService;
import com.platform.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @className: AlgSubjectController
 * @description: 算法控制器
 * @date: 2022/8/8
 * @author: chengqiuming
 */
@RestController
@RequestMapping("alg/subject")
public class AlgSubjectController extends AbstractController {
    @Autowired
    private AlgSubjectService algSubjectService;

    /**
     * 查看所有列表
     *
     * @param params 查询参数
     * @return RestResponse
     */
    @RequestMapping("/queryAll")
    @RequiresPermissions("alg:subject:list")
    public RestResponse queryAll(@RequestParam Map<String, Object> params) {
        List<AlgSubjectEntity> list = algSubjectService.queryAll(params);

        return RestResponse.success().put("list", list);
    }

    /**
     * 分页查询算法题目
     *
     * @param params 查询参数
     * @return RestResponse
     */
    @GetMapping("/list")
    @RequiresPermissions("alg:subject:list")
    public RestResponse list(@RequestParam Map<String, Object> params) {
        Page page = algSubjectService.queryPage(params);

        return RestResponse.success().put("page", page);
    }

    /**
     * 根据主键查询详情
     *
     * @param id 主键
     * @return RestResponse
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("alg:subject:info")
    public RestResponse info(@PathVariable("id") String id) {
        AlgSubjectEntity algSubject = algSubjectService.getById(id);

        return RestResponse.success().put("subject", algSubject);
    }

    /**
     * 新增算法题目
     *
     * @param algSubject 算法题目
     * @return RestResponse
     */
    @SysLog("新增算法题目")
    @RequestMapping("/save")
    @RequiresPermissions("alg:subject:save")
    public RestResponse save(@RequestBody AlgSubjectEntity algSubject) {
        algSubject.setCreateUserId(getUser().getUserName());
        algSubjectService.add(algSubject);

        return RestResponse.success();
    }

    /**
     * 算法计算
     *
     * @param algSubject 算法题目
     * @return RestResponse
     */
    @SysLog("计算")
    @RequestMapping("/cal/{id}")
    @RequiresPermissions("alg:subject:cal")
    public RestResponse cal(@PathVariable("id") String id, @RequestBody AlgSubject algSubject) {
        AlgSubjectEntity algSubjectEntity = algSubjectService.getById(id);
        // 算法编码
        String code = algSubjectEntity.getCode();
        long startTime = new Date().getTime();
        // 输出
        String output = algSubjectService.cal(code, algSubject.getInput());
        long endTime = new Date().getTime();
        algSubject.setOutput(output);
        // 耗时
        Long cost = (endTime - startTime);
        algSubject.setCost(cost.toString());
        return RestResponse.success().put("output", algSubject.getOutput()).put("cost", algSubject.getCost());
    }

    /**
     * 修改算法题目
     *
     * @param algSubject algSubject
     * @return RestResponse
     */
    @SysLog("修改算法题目")
    @RequestMapping("/update")
    @RequiresPermissions("alg:subject:update")
    public RestResponse update(@RequestBody AlgSubjectEntity algSubject) {

        algSubjectService.update(algSubject);

        return RestResponse.success();
    }

    /**
     * 根据主键删除算法题目
     *
     * @param ids ids
     * @return RestResponse
     */
    @SysLog("删除算法题目")
    @RequestMapping("/delete")
    @RequiresPermissions("alg:subject:delete")
    public RestResponse delete(@RequestBody String[] ids) {
        algSubjectService.deleteBatch(ids);

        return RestResponse.success();
    }
}
